package cc.wfu.domain.auth.service;

import cc.wfu.domain.user.model.entity.UserEntity;
import cc.wfu.domain.user.service.IUserService;
import cc.wfu.type.entity.JwtUser;
import cc.wfu.type.exception.UsernamePasswordErrorException;
import cc.wfu.type.interfaces.JwtUserDetails;
import cc.wfu.type.service.UserDetailsServiceStrategy;
import cc.wfu.types.enums.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc:
 * @Date: 2024-05-22-0:13
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsServiceStrategy {

    @Resource
    protected IUserService userService;

    @Override
    public JwtUserDetails loadUserByUsername(String username) throws UsernamePasswordErrorException {
        // 1. 查询用户信息
        UserEntity userEntity = userService.queryUser(username);
        if (userEntity == null) {
            throw new UsernamePasswordErrorException(Constants.ResponseError.USERNAME_NOT_FOUND.getInfo());
        }
        // 2. 查询权限【用户类型】
        String authority = userEntity.getRoleType();
        // 3. 返回
        return new JwtUser(username, userEntity.getPassword(), authority);
    }
}
